package com.project.decoration.controller.server;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationPriceServer;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationPriceServerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 套餐服务controller
 * @date 2025/02/13 09:55
 */
@Controller
@ResponseBody
@RequestMapping("server")
public class DecorationPriceServerController {

    @Autowired
    private DecorationPriceServerService decorationPriceServerService;

    /** 分页获取套餐服务 */
    @PostMapping("getDecorationPriceServerPage")
    public Result getDecorationPriceServerPage(@RequestBody DecorationPriceServer decorationPriceServer) {
        Page<DecorationPriceServer> page = new Page<>(decorationPriceServer.getPageNumber(),decorationPriceServer.getPageSize());
        QueryWrapper<DecorationPriceServer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(decorationPriceServer.getPriceId()),DecorationPriceServer::getPriceId,decorationPriceServer.getPriceId())
                .like(StringUtils.isNotBlank(decorationPriceServer.getContent()),DecorationPriceServer::getContent,decorationPriceServer.getContent());
        Page<DecorationPriceServer> decorationPriceServerPage = decorationPriceServerService.page(page, queryWrapper);
        return Result.success(decorationPriceServerPage);
    }

    @GetMapping("getDecorationPriceServerByPriceId")
    public Result getDecorationPriceServerByPriceId(@RequestParam("priceId") String priceId) {
        QueryWrapper<DecorationPriceServer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DecorationPriceServer::getPriceId,priceId);
        List<DecorationPriceServer> serverList = decorationPriceServerService.list(queryWrapper);
        return Result.success(serverList);
    }

    /** 根据id获取套餐服务 */
    @GetMapping("getDecorationPriceServerById")
    public Result getDecorationPriceServerById(@RequestParam("id")String id) {
        DecorationPriceServer decorationPriceServer = decorationPriceServerService.getById(id);
        return Result.success(decorationPriceServer);
    }

    /** 保存套餐服务 */
    @PostMapping("saveDecorationPriceServer")
    public Result saveDecorationPriceServer(@RequestBody DecorationPriceServer decorationPriceServer) {
        boolean save = decorationPriceServerService.save(decorationPriceServer);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑套餐服务 */
    @PostMapping("editDecorationPriceServer")
    public Result editDecorationPriceServer(@RequestBody DecorationPriceServer decorationPriceServer) {
        boolean save = decorationPriceServerService.updateById(decorationPriceServer);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除套餐服务 */
    @GetMapping("removeDecorationPriceServer")
    public Result removeDecorationPriceServer(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationPriceServerService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("套餐服务id不能为空！");
        }
    }

}
